<?php
/**
 * Description of Category
 *
 * @author cybill
 */

class Admin_Model_DbTable_Category extends Zend_Db_Table_Abstract {
    
    protected $_name = 'category';
    protected $_id = 'catID';
    
    public function getCategory($where, $options) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name)
                ->order("create_on DESC");
        
        if (is_array($where)) {
            foreach ($where as $key => $value) {
                $sql->where($key, $value);
            }
        }
        
        if (is_array($options)) {
            if (isset($options['limit']))
                foreach ($options['limit'] as $key => $value) {
                    $sql->limit($key, $value);
                }
        }
        
        return $this->_db->fetchAll($sql);
    }
    
    public function getAllCategory() {
        $sql = $this->getAdapter()->select()
                ->from($this->_name)
                ->order("name DESC");
        
        return $this->_db->fetchAll($sql);
    }
    
    public function getCategoryById($id) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name)
                ->where("$this->_id = ?", $id);
        
        return $this->_db->fetchRow($sql);
    }
    
    public function getCategoryByName($name) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name)
                ->where('name = ?', $name);        
        
        return $this->_db->fetchOne($sql);    
    }
    
    public function getCategoryID() {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, array('catID'));
        
        return $this->_db->fetchAll($sql);
    }
    
    public function countRows($where) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, 'COUNT(catID)');                
        
        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
        
        return $this->getAdapter()->fetchOne($sql);
    }
    
    public function doSave($data = array()) {
        return $this->getAdapter()->insert($this->_name, $data);
    }
    
    public function doUpdate($data = array(), $where = '') {
        return $this->getAdapter()->update($this->_name, $data, array("$this->_id = ?"=>$where));
    }
    
    public function doDelete($where) {
        return $this->getAdapter()->delete($this->_name, array("$this->_id = ?" => $where));
    }
}